/*
 * This file is part of the Boring Engine's Kirby Game.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package kirby.misc;

import com.googlecode.boringengine.Graphics;
import com.googlecode.boringengine.Render;
import com.googlecode.boringengine.lua.LuaInterpreter;
import kirby.GameObject;
import kirby.Kirby;
import kirby.Resources;

public class ScriptArea extends GameObject {
    
    private String scriptFileName;
    private short timeout = 0;
    private short timeoutMax = 300;

    public ScriptArea(int x, int y, String[] tokens) {
        this.x = x;
        this.y = y;
        w = Integer.parseInt(tokens[0]);
        h = Integer.parseInt(tokens[1]);
        scriptFileName = tokens[2];
        if (tokens.length >= 4) timeoutMax = Short.parseShort(tokens[3]);
    }

    @Override
    public void die() { }

    @Override
    public void doCollide(GameObject obj) {
        if (timeout != 0) return;
        if (!(obj instanceof Kirby)) return;
        LuaInterpreter.runScript(this, "resources/script/" + scriptFileName + ".lua", Resources.l);
        timeout = timeoutMax;
    }

    @Override
    public void draw() {
        int alpha = Render.getAlpha();
        Render.setAlpha(37);
        Graphics.drawRect(x, y, w, h, 0);
        Render.setAlpha(alpha);
    }

    @Override
    public boolean isEdible() { return false; }

    @Override
    public String save() { return ""; }

    @Override
    public void update0() {
        if (timeout > 0)
            timeout--;
    }

}
